<?php
//此页面要进修复
// AJAX搜索处理函数


if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}



defined('ABSPATH') or die('No script kiddies please!');

// Load WordPress core
require_once(ABSPATH . 'wp-load.php');
require_once(ABSPATH . 'wp-includes/pluggable.php');

new ZB_Ajax();

/**
 * 主题AJAX接口
 * 地址：domain/wp-admin/admin-ajax.php
 * 参数：action 接口
 * 参数：nonce 安全验证参数 使用 wp_create_nonce("zb_ajax") 方法生成
 * 必须区分全部用户可用、登录用户可用、未登录用户可用进行区分验证
 * trim(sanitize_text_field($data))
 */
class ZB_Ajax {

    private $_ajax_prefix        = 'wp_ajax_zb_'; //前缀
    private $_ajax_nopriv_prefix = 'wp_ajax_nopriv_zb_'; //未登录前缀

    public function __construct() {
        $this->load_action();
    }

    //添加钩子 传入方法 type -1全部 0未登录 1登录
    private function add_action($hook_name, $type = null) {
         add_action($this->_ajax_prefix . $hook_name, array($this, $hook_name));
         add_action($this->_ajax_nopriv_prefix . $hook_name, array($this, $hook_name));
        // if ($type === null || $type === 1) {
        //     add_action($this->_ajax_prefix . $hook_name, array($this, $hook_name));
        // }
        // if ($type == null || $type === 0) {
        //     add_action($this->_ajax_nopriv_prefix . $hook_name, array($this, $hook_name));
        // }

    }

    //站内安全nonce验证
    private function valid_nonce_ajax() {

        if (!check_ajax_referer('zb_ajax', 'nonce', false)) {
            wp_send_json(array(
                'status' => 0,
                'msg'    => __('非法请求', 'ripro'),
            ));
        }
    }

    //获取响应参数
    private function get_param($key, $default = '', $method = 'post') {
        return get_param($key, $default = '', $method = 'post');
    }

    //初始化接口集合挂载
    private function load_action() {
        $this->add_action('ajax_comment'); //ajax评论
        $this->add_action('get_captcha_img'); //验证码
        $this->add_action('send_mail_captcha_code'); //发送邮箱验证码，下面没有函数要实现
        $this->add_action('business_ajax_search'); //AJAX搜索功能
    }



    public function business_ajax_search() {
        #此处写AJAX搜索功能
    }



    //发送邮箱验证码
    public function send_mail_captcha_code() {
        $this->valid_nonce_ajax(); #安全验证

        $email = sanitize_email(get_param('email'));

        if (!is_email($email)) {
            wp_send_json(array(
                'status' => 0,
                'msg'    => __('邮箱地址格式错误', 'ripro'),
            ));
        }

        if (email_exists($email)) {
            wp_send_json(array(
                'status' => 0,
                'msg'    => __('此邮箱已被注册', 'ripro'),
            ));
        }

        if (!empty($email) && send_mail_captcha_code($email)) {
            wp_send_json(array(
                'status' => 1,
                'msg'    => __('发送成功', 'ripro'),
            ));
        }
        
        wp_send_json(array(
            'status' => 0,
            'msg'    => __('发送失败', 'ripro'),
        ));
    }


    //图片验证码接口
    public function get_captcha_img() {
        $this->valid_nonce_ajax(); #安全验证
        wp_send_json(array(
            'status' => 1,
            'msg'    => get_img_captcha(), #返回图片验证码
        ));
    }

   



    
    
    public function ajax_comment() {

        //此次写ajax提交评论的逻辑

        echo "success";exit;

    }







}